
import { invoke } from '@tauri-apps/api/core';
import { DeleteCondition, DeleteOptions, QueryCondition, QueryExtention, QueryOptions, ComparsionOperator, UpdateCondition, UpdateOptions } from '../types';
import optionsWrap from '@/components/datasets/create/website/base/options-wrap';
import { DataSource } from '@/services/data-source';
import { dsDelete, dsFind, dsInsert, dsUpdate } from '../datasource';
import { APPLICATION_CONVERSATION_CLASS_IDENTIFIER, APPLICATION_CONVERSATION_MESSAGE_CLASS_IDENTIFIER } from '@/services/constants';
import { appDatasource } from './application';

export interface ApplicationConversation {
    id?: number,
    application_identifier: string,
    identifier: string,
    name: string,
    title: string,
    icon?: string,
    is_pinned: boolean,
    inputs: string[],
    status: 'normal',
    description: string,
}

export const appFetchConversations = async (identifier: string, isInstalled: boolean, pinned: boolean): Promise<{ total: number, data: ApplicationConversation[] }> => {

    let data: any = await dsFind(await appDatasource(identifier, isInstalled), APPLICATION_CONVERSATION_CLASS_IDENTIFIER, {
        attributes: [{
            name: 'application_identifier',
            operator: ComparsionOperator.Equal,
            value: identifier
        }, {
            name: 'is_pinned',
            operator: ComparsionOperator.Equal,
            value: pinned + ''
        }]
    });
    data.data = data.data.map((item) => { return { ...item, id: item.identifier, } })
    console.log(data);
    return data;
}


export const appPinConversation = async (identifier: string, isInstalled: boolean, conversationId: string): Promise<void> => {
    console.log('appInsertConversation', identifier, isInstalled, conversationId);

    await dsUpdate(await appDatasource(identifier, isInstalled), APPLICATION_CONVERSATION_CLASS_IDENTIFIER, {
        attributes: [{
            name: 'identifier',
            operator: ComparsionOperator.Equal,
            value: conversationId
        }]
    }, {
        'is_pinned': true
    });
    return;
}

export const appRenameConversation = async (identifier: string, isInstalled: boolean, conversationId: string, newName: string): Promise<void> => {
    console.log('appRenameConversation', identifier, isInstalled, conversationId, newName);

    await dsUpdate(await appDatasource(identifier, isInstalled), APPLICATION_CONVERSATION_CLASS_IDENTIFIER, {
        attributes: [{
            name: 'identifier',
            operator: ComparsionOperator.Equal,
            value: conversationId
        }]
    }, {
        'title': newName,
        'name': newName,
    });
    return;
}

export const appRemoveConversation = async (identifier: string, isInstalled: boolean, conversationId: string): Promise<any[]> => {
    console.log('appRemoveConversation', identifier, isInstalled, conversationId);

    let data: any = await dsDelete(await appDatasource(identifier, isInstalled), APPLICATION_CONVERSATION_CLASS_IDENTIFIER, {
        attributes: [{
            name: 'identifier',
            operator: ComparsionOperator.Equal,
            value: conversationId
        }]
    });

    console.log(data);
    return data;
}

export const appUnpinConversation = async (identifier: string, isInstalled: boolean, conversationId: string): Promise<void> => {
    console.log('appUnpinConversation', identifier, isInstalled, conversationId);
    await dsUpdate(await appDatasource(identifier, isInstalled), APPLICATION_CONVERSATION_CLASS_IDENTIFIER, {
        attributes: [{
            name: 'identifier',
            operator: ComparsionOperator.Equal,
            value: conversationId
        }]
    }, {
        'is_pinned': false
    });
}


export const appInsertConversation = async (identifier: string, isInstalled: boolean, item: ApplicationConversation): Promise<any[]> => {
    console.log('appInsertConversation', identifier, isInstalled, item);

    let data: any = await dsInsert(await appDatasource(identifier, isInstalled), APPLICATION_CONVERSATION_CLASS_IDENTIFIER, item);

    console.log(data);
    return data;
}

export default {}